
package com.eworking.eim.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smack.RosterStorage;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item;

import com.eworking.eim.EIMSessionManager;
import com.eworking.eim.util.EIMLog;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

public class EIMRosterStorage implements RosterStorage {
    
    /** 日志输出标记 */
    private static final String _TAG = EIMRosterStorage.class.getSimpleName();
    
    private static final String _KEY_ROSTER_VERSION = "eworking.im.roster.";
    
    private Context context = null;
    
    private EIMUserManager userManager = null;
    
    private String version;
    
    private List<RosterPacket.Item> rosterItems = new ArrayList<RosterPacket.Item>();
    
    public EIMRosterStorage(Context context, EIMUserManager userManager) {
        this.context = context;
        this.userManager = userManager;
        this.loadEntries();
    }
    
    private void loadEntries() {
        Iterator<EIMUser> iterator = this.userManager.getUserTable().values().iterator();
        while (iterator.hasNext()) {
            EIMUser user = iterator.next();
            this.rosterItems.add(new RosterPacket.Item(user.getJID(), user.getAccount()));
        }
    }
    
    @Override
    public void addEntry(Item item, String version) {
        if (TextUtils.isEmpty(version) && !version.equals(this.version)) {
            this.updateRosterVersion(version);
        }
    }
    
    @Override
    public List<Item> getEntries() {
        return this.rosterItems;
    }
    
    @Override
    public Item getEntry(String account) {
        Iterator<Item> iterator = this.rosterItems.iterator();
        while (iterator.hasNext()) {
            Item item = iterator.next();
            if (item.getName().equals(account)) {
                return item;
            }
        }
        EIMLog.d(_TAG, "获取好友" + account);
        return null;
    }
    
    @Override
    public int getEntryCount() {
        return this.rosterItems.size();
    }
    
    @Override
    public String getRosterVersion() {
        if (this.version == null) {
            String key = _KEY_ROSTER_VERSION + EIMSessionManager.getInstance(context).getCurrentUser().JID;
            this.version = PreferenceManager.getDefaultSharedPreferences(this.context).getString(key, "");
        }
        return this.version;
    }
    
    @Override
    public void removeEntry(String arg0) {
        
    }
    
    @Override
    public void updateLocalEntry(Item arg0) {
        
    }
    
    private void updateRosterVersion(String version) {
        this.version = version;
        String key = _KEY_ROSTER_VERSION + EIMSessionManager.getInstance(context).getCurrentUser().JID;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.context);
        preferences.edit().putString(key, version);
        preferences.edit().commit();
    }
}
